package reflectDemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo {
    public static void reflectNewInstance(){
        Class<?>c1;
        try {
            c1=Class.forName("reflectDemo.Student");
            Student student=(Student) c1.newInstance();
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    //反射私有的构造方法
    public static void reflectPrivateConstructor(){
        Class<?>c;
        try {
            c=Class.forName("reflectDemo.Student");
            Constructor<Student> con =(Constructor) c.getDeclaredConstructor(String.class,int.class);
            con.setAccessible(true);
            Student student=con.newInstance("zhangsan",18);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    //反射私有属性
    public static void reflectPrivateField(){
        Class<?>c;
        try {
            c=Class.forName("reflectDemo.Student");
            Field field=c.getDeclaredField("name");
            field.setAccessible(true);
            Student student=(Student) c.newInstance();
            field.set(student,"王五");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    //反射私有方法
    public static void reflectPrivateMethod() {
        Class<?>c1;
        try {
            c1=Class.forName("reflectDemo.Student");
            Method method=c1.getDeclaredMethod("function", String.class);
            Student student=(Student)c1.newInstance();
            method.setAccessible(true);
            method.invoke(student,"我是一个参数");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
       // reflectNewInstance();
       //reflectPrivateConstructor();
      //  reflectPrivateField();
        reflectPrivateMethod();
    }

}
